package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.CollectActivity;
import flc.ast.activity.DailyActivity;
import flc.ast.activity.SearchActivity;
import flc.ast.activity.TextActivity;
import flc.ast.activity.TextAssistantActivity;
import flc.ast.adapter.DailyAdapter;
import flc.ast.adapter.HomeTextAdapter;
import flc.ast.adapter.HomeTypeAdapter;
import flc.ast.bean.TypeBean;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IntentUtil;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private DailyAdapter dailyAdapter;
    private HomeTypeAdapter typeAdapter;
    private HomeTextAdapter textAdapter;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);


        mDataBinding.ivSearch.setOnClickListener(this);
        mDataBinding.ivCollect.setOnClickListener(this);
        mDataBinding.tvTextAssistant.setOnClickListener(this);

        mDataBinding.rvDailyList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        dailyAdapter = new DailyAdapter();
        mDataBinding.rvDailyList.setAdapter(dailyAdapter);
        dailyAdapter.addChildClickViewIds(R.id.ivDailyItemCopy);
        dailyAdapter.addChildClickViewIds(R.id.ivDailyItemShare);
        dailyAdapter.setOnItemChildClickListener(this);
        dailyAdapter.setOnItemClickListener(this);

        mDataBinding.rvTypeList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        typeAdapter = new HomeTypeAdapter();
        mDataBinding.rvTypeList.setAdapter(typeAdapter);
        typeAdapter.setOnItemClickListener(this);

        mDataBinding.rvTextList.setLayoutManager(new GridLayoutManager(mContext, 2));
        textAdapter = new HomeTextAdapter();
        mDataBinding.rvTextList.setAdapter(textAdapter);
        textAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSearch://搜索
                startActivity(SearchActivity.class);
                break;
            case R.id.ivCollect://收藏
                startActivity(CollectActivity.class);
                break;
            case R.id.tvTextAssistant://文案助手
                startActivity(TextAssistantActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == dailyAdapter) {
            DailyActivity.stkResBean = dailyAdapter.getItem(position);
            DailyActivity.type = position;
            startActivity(DailyActivity.class);
        } else if (adapter == typeAdapter) {
            typeAdapter.getItem(oldPosition).setSelected(false);
            typeAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            typeAdapter.getItem(position).setSelected(true);
            typeAdapter.notifyItemChanged(position);
            getData(typeAdapter.getItem(position).getHashid());
        } else if (adapter == textAdapter) {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            TextActivity.stkResBean = bean;
            startActivity(TextActivity.class);
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivDailyItemCopy://复制
                ClipboardUtils.copyText(dailyAdapter.getItem(position).getDesc());
                ToastUtils.showShort(getString(R.string.copy_success_tips));
                break;
            case R.id.ivDailyItemShare://分享
                IntentUtil.shareText(mContext, dailyAdapter.getItem(position).getDesc());
                break;
        }
    }

    @Override
    protected void initData() {
        getDailyData();
        getTypeData();
    }

    private void getDailyData() {
        StkResApi.getTagResourceList(this, AppConfig.DAILY_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        dailyAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getTypeData() {
        StkResApi.getChildTagList(this, AppConfig.TYPE_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        List<TypeBean> list = new ArrayList<>();
                        for (StkTagResBean stkTagResBean : result) {
                            list.add(new TypeBean(stkTagResBean.getName(), stkTagResBean.getHashid()));
                        }
                        list.get(0).setSelected(true);
                        typeAdapter.setList(list);
                        getData(typeAdapter.getItem(0).getHashid());
                    }
                }
            }
        });
    }

    private void getData(String url) {
        StkResApi.getTagResourceList(this, AppConfig.URL_PREFIX + url, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        textAdapter.setList(result);
                    }
                }
            }
        });
    }



}